<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleCheck()" v-auth="'setting:pricemodel:wecharPriceModel:edit'">
          <Icon icon="simple-line-icons:shuffle" /> {{ t('数据合法性检查') }}
        </a-button>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'setting:pricemodel:wecharPriceModel:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <WecharPriceModelInv @register="registerDrawer2" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSettingPricemodelWecharPriceModelList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { wecharPriceModelDelete, wecharPriceModelListData } from '/@/api/setting/pricemodel/wecharPriceModel';
  import { wecharPriceModelDisable, wecharPriceModelEnable } from '/@/api/setting/pricemodel/wecharPriceModel';
  import { wecharPriceSystemTreeData } from '/@/api/setting/pricemodel/wecharPriceSystem';
  import { wecharPriceModelInvCheck  } from '/@/api/setting/pricemodel/wecharPriceModelInv';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import WecharPriceModelInv from '../wecharPriceModelInv/list.vue';
  import FormImport from './formImport.vue';
  

  const { t } = useI18n('setting.pricemodel.wecharPriceModel');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('价格模板管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('模板编号'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('模板名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('价格体系'),
        field: 'sysCode',
        // fieldLabel: 'price.name',
        component: 'TreeSelect',
        componentProps: {
          api: wecharPriceSystemTreeData,
          params: {status:'0' },
          allowClear: true,
        },
      },
      {
        label: t('创建人'),
        field: 'createByName',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('模板说明'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('模板编号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 100,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('模板名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('价格体系'),
      dataIndex: 'priceSys.name',
      key: 'a.sys_code',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('模板说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('创建人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 70,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'sys_search_status',
    },
    
    
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑价格模板'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'setting:pricemodel:wecharPriceModel:edit',
      },
      {
        icon: 'ant-design:table-outlined',
        // color: 'success',
        title: t('编辑价格模板子表'),
        onClick: handleTable.bind(this, record),
        auth: 'setting:pricemodel:wecharPriceModel:edit',
      },
      {
        icon: 'ant-design:stop-outlined',
        color: 'error',
        title: t('停用价格模板'),
        popConfirm: {
          title: t('是否确认停用价格模板'),
          confirm: handleDisable.bind(this, { code: record.code }),
        },
        auth: 'setting:pricemodel:wecharPriceModel:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用价格模板'),
        popConfirm: {
          title: t('是否确认启用价格模板'),
          confirm: handleEnable.bind(this, { code: record.code }),
        },
        auth: 'setting:pricemodel:wecharPriceModel:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除价格模板'),
        popConfirm: {
          title: t('是否确认删除价格模板'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'setting:pricemodel:wecharPriceModel:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawer2, { openDrawer:openDrawer2 }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: wecharPriceModelListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    ellipsis:false,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  // 表格弹窗
  function handleTable(record: Recordable) {
    openDrawer2(true, record);
  }

  async function handleDisable(record: Recordable) {
    const res = await wecharPriceModelDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await wecharPriceModelEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await wecharPriceModelDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleCheck() {

    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.code + ',';
      });
      const res = await wecharPriceModelInvCheck({selIds:selIds.value});
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  function handleSuccess() {
    reload();
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    // downloadByUrl({
    //   url: ctxAdminPath + '/setting/pricemodel/wecharPriceModel/exportData',
    //   target: '_self',
    // });

    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.code + ',';
      });
      downloadByUrl({
        url: ctxAdminPath + '/setting/pricemodel/wecharPriceModel/exportData?selIds=' + selIds.value,
        target: '_self',
      });
    } else {
      showMessage('请选择需导出数据！！');
    }
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }
</script>
<style lang="less" scoped>
  /deep/.jeesite-basic-arrow{
    display: none !important;
  }
</style>
